/*
 * $Id: RscAction.java,v 1.1 2001/10/06 12:32:34 jppietsch Exp $
 * Copyright (C) 2001 Jan-Peter Pietsch
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.jppietsch.gui;


import de.jppietsch.error.Log;
import de.jppietsch.error.Precondition;
import de.jppietsch.util.RscBundle;

import javax.swing.*;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Level;


/**
 * An action whose values are localized by means of a resource bundle.
 *
 * @author Jan-Peter Pietsch
 */
public abstract class RscAction extends AbstractAction {

    /**
     * Localizes this action from the given resource bundle.
     *
     * @param aBundle contains localized properties of this action.
     * @param aPrefix used to concatenate the resource keys.
     */
    protected RscAction(RscBundle aBundle, String aPrefix) {
        Precondition.argumentNotNull("aBundle", aBundle);
        Precondition.argumentStringNotEmpty("aKey", aPrefix);
        String name = aBundle.getString(aPrefix + RscBundle.NAME);

        putValue(DEFAULT, aPrefix);
        putValue(NAME, name);
        putOptionalValue(SHORT_DESCRIPTION, aBundle, RscBundle.SHORT, name);
        putOptionalValue(LONG_DESCRIPTION, aBundle, RscBundle.SHORT,
                getValue(SHORT_DESCRIPTION));
        putOptionalValue(SMALL_ICON, aBundle, RscBundle.ICON);
        putOptionalValue(ACTION_COMMAND_KEY, aBundle, RscBundle.CMD, aPrefix);
        putOptionalValue(ACCELERATOR_KEY, aBundle, RscBundle.ACCEL);
        putOptionalValue(MNEMONIC_KEY, aBundle, RscBundle.MNEMO);
    }

    protected RscAction(Class aResourceBundleClass, String aKey) {
        this(RscBundle.getBundle(aResourceBundleClass), aKey);
    }

    private void putOptionalValue(String aKey,
                                  ResourceBundle aBundle,
                                  String aResourceKeySuffix,
                                  Object aDefaultValue) {
        try {
            putValue(aKey, aBundle, aResourceKeySuffix);
        } catch (MissingResourceException exception) {
            Log.LOG.logp(
                    Level.FINE,
                    RscAction.class.getName(),
                    "putOptionalValue(String,ResourceBundle,String,Object)",
                    "Resource " + resourceKey(aResourceKeySuffix) + " not found in " +
                            aBundle + ", using " + aDefaultValue + " as default.");
            putValue(aKey, aDefaultValue);
        }
    }

    private void putOptionalValue(String aKey,
                                  ResourceBundle aBundle,
                                  String aResourceKeySuffix) {
        try {
            putValue(aKey, aBundle, aResourceKeySuffix);
        } catch (MissingResourceException exception) {
            Log.LOG.logp(
                    Level.FINE,
                    RscAction.class.getName(),
                    "putOptionalValue(String,ResourceBundle,String)",
                    "Resource " + resourceKey(aResourceKeySuffix) + " not found in " +
                            aBundle + ".");
        }
    }

    private void putValue(String aKey,
                          ResourceBundle aBundle,
                          String aResourceKeySuffix) {
        String key = resourceKey(aResourceKeySuffix);
        Object value = aBundle.getObject(key);

        putValue(aKey, value);
    }

    private String resourceKey(String aResourceKeySuffix) {
        return getValue(DEFAULT).toString() + aResourceKeySuffix;
    }

}